package com.chushouya.manager.service.admin.impl;

import com.chushouya.common.support.Contexts;
import com.chushouya.manager.dto.admin.partner.*;
import com.general.framework.core.enums.DelFlagEnum;
import com.general.framework.core.enums.StatusEnum;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.schema.PageResult;
import com.chushouya.manager.dao.entity.PartnerEntity;
import com.chushouya.manager.dao.entity.PartnerPromoteEntity;
import com.chushouya.manager.dao.repository.PartnerPromoteRepository;
import com.chushouya.manager.dao.repository.PartnerRepository;
//=====manager.dto.admin.partner.*;
import com.chushouya.manager.dto.admin.partner.*;
import com.chushouya.manager.service.admin.PartnerPromoteService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class PartnerPromoteServiceImpl implements PartnerPromoteService {

    @Resource
    private PartnerPromoteRepository partnerPromoteRepository;

    @Resource
    private PartnerRepository partnerRepository;

    @Override
    public PageResult<PartnerPromoteListDTO> selectPage(PartnerPromoteQuery query) {
        return PageResult.of(partnerPromoteRepository.selectPageResult(query), PartnerPromoteListDTO.class);
    }

    @Override
    public PartnerPromoteDetailDTO detail(Long promoteId) {
        return Beans.copy(partnerPromoteRepository.selectByPrimaryKey(promoteId), PartnerPromoteDetailDTO.class);
    }

    @Override
    public void save(PartnerPromoteSaveDTO dto) {
        PartnerPromoteQuery query = new PartnerPromoteQuery();
        query.setPromoteLabel(dto.getPromoteLabel());
        PartnerPromoteEntity exits = partnerPromoteRepository.selectOne(query);
        Check.isNull(exits, "推广标签重复:{}", dto.getPromoteLabel());

        PartnerEntity partnerEntity = partnerRepository.selectByPrimaryKey(dto.getPartnerId());
        Check.notNull(partnerEntity, "合作商不存在");

        final PartnerPromoteEntity entity = Beans.copy(dto, PartnerPromoteEntity.class);
        entity.setPartnerName(partnerEntity.getPartnerName());
        entity.setPartnerType(partnerEntity.getPartnerType());
        entity.setStatus(StatusEnum.normal.value());
        entity.setHandleUser(Contexts.getUserContext().getUserName());
        entity.setCreateTime(Dates.getTimeNow());
        entity.setUpdateTime(Dates.getTimeNow());
        partnerPromoteRepository.insertSelective(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(Long promoteId) {
        PartnerPromoteEntity update = new PartnerPromoteEntity();
        update.setPromoteId(promoteId);
        update.setDelFlag(DelFlagEnum.delete.getValue());
        partnerPromoteRepository.updateByPrimaryKeySelective(update);
    }

    @Override
    public void update(PartnerPromoteUpdateDTO dto) {
        PartnerPromoteEntity update = Beans.copy(dto, PartnerPromoteEntity.class);
        update.setUpdateTime(Dates.getTimeNow());
        partnerPromoteRepository.updateByPrimaryKeySelective(update);
    }

    @Override
    public Boolean switchStatus(Long promoteId) {
        final PartnerPromoteEntity partnerPromoteEntity = partnerPromoteRepository.selectByPrimaryKey(promoteId);
        Check.notNull(partnerPromoteEntity, "数据不存在");

        PartnerPromoteEntity update = new PartnerPromoteEntity();
        update.setPromoteId(promoteId);
        update.setUpdateTime(Dates.getTimeNow());
        update.setStatus(Objects.equals(partnerPromoteEntity.getStatus(), StatusEnum.normal.value()) ? StatusEnum.disable.value() : StatusEnum.normal.value());
        partnerPromoteRepository.updateByPrimaryKeySelective(update);
        return Boolean.TRUE;
    }

    @Override
    public PartnerPromoteEntity selectOneByPromoteLabel(String promoteLabel) {
        PartnerPromoteQuery query = new PartnerPromoteQuery();
        query.setPromoteLabel(promoteLabel);
        return partnerPromoteRepository.selectOne(query);
    }
}
